clear all 
set varabbrev off, permanently
use epfipc2006-2019.dta
*
* Create a panel dataset
* 
* This drops all hh that collaborate just once
drop if id_hh==.
label variable id_hh "Household id"
* Year
*
gen year=ANOENC
tab ANOENC, gen(year)
* Drop 138 observation with missing year
drop if ANOENC==. 
* this results in an unbalanced panel 
xtset id_hh year, yearly
*
* Postreatment period identifier
*
gen post=(CLAVE==2)
label variable post "post-treatment period = 1"
*
* HOUSEHOLD COVARIATES
*
* Regional dummies
*
* Pooling together Ceuta & Melilla
replace CCAA=18 if CCAA==18 | CCAA==19
gen ccaa=CCAA
tab CCAA, gen(ccaa)
*
* Town size
*
tab TAMAMU, gen(tsize)
*
* Population density dummies
*
tab DENSI, gen(pdensity)
*
* Household size
*
gen nmembers=NMIEMB
label variable nmembers "Number of household members"
gen nmembaeq=UC2
label variable nmembaeq "Number of adult equivalent members"
*
* Domestic workers
*
gen ndworkers=NMIEMSD
label variable ndworkers "Number of workers"
*
* Household type
*
gen coupleplus=(TIPHOGAR_7==3 | TIPHOGAR_7==4 | TIPHOGAR_7==5 | TIPHOGAR_7==6)
*
* MAIN BREADWINNER COVARIATES
*
* Age
*
gen agemb=EDADSP
label variable agemb "Age Main breadwinner"
*
* Gender
*
gen femalemb=(SEXOSP==6)
label variable femalemb "Female Main breadwinner = 1"
* 
* Nationality 
*
gen foreignmb=(NACIONA_SP==2)
label variable foreignmb "Foreign Main breadwinner = 1"
*
* Education (only available from 2011 on)
*
tab ESTUDRED_SP, gen(studmb)
*
* Labor status 
*
gen unemployedmb=(SITUACT_SP==3)
replace unemployedmb=. if SITUACT_SP==.
gen employedmb=(SITUACT_SP==1 | SITUACT_SP==2)
gen activemb=(SITUACT_SP==1 | SITUACT_SP==2 | SITUACT_SP==3)
gen inactivemb=(SITUACT_SP==4 | SITUACT_SP==5 | SITUACT_SP==6 | SITUACT_SP==7 | SITUACT_SP==8)
gen retiredmb=(SITUACT_SP==4)
gen ptimemb=(JORNADASP==2)
replace ptimemb=. if (JORNADASP==-9 | JORNADASP==.)
gen ftimemb=(JORNADASP==1)
replace ftimemb=. if (JORNADASP==-9 | JORNADASP==.)
*
* Labor status from the household members file
*
gen unemployedhousehead=.
gen employedhousehead=.
gen inactivehousehead=.
gen activehousehead=.
gen retiredhousehead=.
gen ptimehousehead=.
gen ftimehousehead=.
forvalues member=1(1)18 {
	if RELASP`member'==1 {
		replace unemployedhousehead=(SITUACT`member'==3)
		replace unemployedhousehead=. if SITUACT`member'==.
		replace employedhousehead=(SITUACT`member'==1 | SITUACT`member'==2)
		replace employedhousehead=. if SITUACT`member'==.
		replace retiredhousehead=(SITUACT`member'==4)
		replace retiredhousehead=. if SITUACT`member'==-9 | SITUACT`member'==.
		replace ptimehousehead=(JORNADA`member'==2) if ANOENC>2010
		replace ptimehousehead=. if SITUACT`member'==-9 | SITUACT`member'==.
		replace ftimehousehead=(JORNADA`member'==1) if ANOENC>2010
		replace ftimehousehead=. if SITUACT`member'==-9 | SITUACT`member'==.
	}
}
*
* Income
*
gen incomemb=IMPEXACP_SP
replace incomemb=. if IMPEXACP_SP==-9
*
* Occupation
* 
replace OCUPARED=. if OCUPARED==-9
tab OCUPARED, gen(occupation)
*
* Sector (1st, 2nd, 3rd) / This variable has two different codifications (up to 2008 and from 2009) omit in analysis
*
replace ACTESTB_RED=. if ACTESTB_RED==-9
tab ACTESTB_RED, gen(sector)
*
* Professional situation
*
replace SITPROF=. if SITPROF==-9
tab SITPROF, gen(situation)
*
* Private / public sector
*
gen pubsec=(SECTOR==1)
replace pubsec=. if SECTOR==-9 | SECTOR==.
*
* Term contract
*
gen temp=(TIPOCONT==2)
replace temp=. if TIPOCONT==-9 | TIPOCONT==.
*
* Permanent contract
*
gen perm=(TIPOCONT==1)
replace perm=. if TIPOCONT==-9 | TIPOCONT==.
*
* SPOUSE COVARIATES
*
* Age
*
gen agespouse=.
forvalues member=1(1)18 {
	replace agespouse=EDAD`member' if RELASP`member'==2
}
*
* Gender
*
gen femalespouse=.
forvalues member=1(1)18 {
	replace femalespouse=(SEXO`member'==6) if RELASP`member'==2
}
* 
* Nationality 
*
gen foreignspouse=.
forvalues member=1(1)18 {
	replace foreignspouse=(NACIONA`member'==2) if RELASP`member'==2
}
*
* Education (only available from 2011 on)
*
gen studredspouse=.
forvalues member=1(1)18 {
	replace studredspouse=ESTUDRED`member' if RELASP`member'==2
	replace studredspouse=. if ESTUDRED`member'==-9
}
tab studredspouse, gen(studspouse)
*
* Labor status 
*
gen unemployedspouse=.
gen employedspouse=.
gen inactivespouse=.
gen activespouse=.
gen retiredsp=.
gen ptimespouse=.
gen ftimespouse=.
forvalues member=1(1)18 {
	if RELASP`member'==2 {
		replace unemployedspouse=(SITUACT`member'==3)
		replace unemployedspouse=. if SITUACT`member'!=-9 & SITUACT`member'!=.
		replace employedspouse=(SITUACT`member'==1 | SITUACT`member'==2) 
		replace employedspouse=. if SITUACT`member'!=-9 & SITUACT`member'!=.
		replace retiredsp=(SITUACT`member'==4)
		replace retiredsp=. if SITUACT`member'==-9 | SITUACT`member'==.
		replace ptimespouse=(JORNADA`member'==2) if ANOENC>2010
		replace ptimespouse=. if (JORNADA`member'==-9 | JORNADA`member'==.)
		replace ftimespouse=(JORNADA`member'==1) if ANOENC>2010
		replace ftimespouse=. if (JORNADA`member'==-9 | JORNADA`member'==.)
	}
}
*
* Income
*
gen incomespouse=.
forvalues member=1(1)18 {
	replace incomespouse=IMPEXACP`member' if RELASP`member'==2
	replace incomespouse=. if IMPEXACP`member'==-9
}
*
* HOUSE of RESIDENCE COVARIATES
*
* Type of property tenure (Own, rent, gift, )
*
replace REGTEN=. if REGTEN==-9
tab REGTEN, gen(ttenure)
*
* Number of rooms
*
replace NHABIT=. if NHABIT==-9
gen nrooms=NHABIT
*
* Squared meters
*
replace SUPERF=. if SUPERF==-9
gen squaredm=SUPERF
*
* Residential area type
*
replace ZONARES=. if ZONARES==-9
tab ZONARES, gen(resid)
gen residshort=(ZONARES==1 | ZONARES==2 | ZONARES==3 | ZONARES==4)
* Urban residential area residshort=1, rural residential area residshort=0
replace residshort=. if ZONARES==.
*
* Other houses
*
gen othehouse=(DISPOSI_OV==1 & (REGTENV1==1 | REGTENV1==2))
replace othehouse=. if REGTENV1==-9
*
* HOUSEHOLD INCOME
*
* Labor income
*
gen labinc=(CAPROP==1 | CAJENA==1)
replace labinc=. if CAPROP==-9 | CAJENA==-9
*
* Pension income
*
gen peninc=(PENSIO==1)
replace peninc=. if PENSIO==-9
*
* Unemployment benefits
*
gen uneinc=(DESEM==1)
replace uneinc=. if DESEM==-9
* 
* Other social benefits
*
gen othinc=(OTRSUB==1)
replace othinc=. if OTRSUB==-9
*
* Capital income
* 
gen capinc=(RENTAS==1)
replace capinc=. if RENTAS==-9
*
* Monthly income
*
replace IMPEXAC=. if IMPEXAC==-9
gen income=IMPEXAC
*
* Income bracket
*
replace INTERIN=. if INTERIN==-9
tab INTERIN, gen(incbra)
*
* HOUSEHOLD MEMBER COVARIATES
*
* For some household member categorical covariates the sum of number of household members for all categories 
* does not add up to the total number of household members because data are missing
* 
* Number of HH members by Age groups
*
gen n0_4=0
gen n5_15=0 
gen n16_24=0 
gen n25_34=0 
gen n35_64=0 
gen n65_79=0 
gen n80omas=0
gen n50ormore=0
forvalues member=1(1)18 {
	replace n0_4=n0_4+(EDAD`member'<5 & EDAD`member'!=.)
	replace n5_15=n5_15+(EDAD`member'>4 & EDAD`member'<16)
	replace n16_24=n16_24+(EDAD`member'>15 & EDAD`member'<25)
	replace n25_34=n25_34+(EDAD`member'>24 & EDAD`member'<35)
	replace n35_64=n35_64+(EDAD`member'>34 & EDAD`member'<65)
	replace n65_79=n65_79+(EDAD`member'>64 & EDAD`member'<80)
	replace n80omas=n80omas+(EDAD`member'>79 & EDAD`member'!=.)
	replace n50ormore=n50ormore+(EDAD`member'>49 & EDAD`member'!=.)
}
* 
* Number of children
*
gen nchildren=0
gen nborn=0
forvalues member=1(1)18 {
	replace nchildren=nchildren+(RELASP`member'==3)
	replace nborn=nborn+(EDAD`member'<1 & RELASP`member'==3)
}
*
* Number of women and men
*
gen nfemales=0
gen nmales=0
forvalues member=1(1)18 {
	replace nfemales=nfemales+(SEXO`member'==6)
	replace nmales=nmales+(SEXO`member'==1)
}
*
* Number of foreigners
*
gen nforeign=0
forvalues member=1(1)18 {
	replace nforeign=nforeign+(NACIONA`member'==2)
}
*
* Education for household members 16 or older
*
gen nstudies1=0
gen nstudies2=0
gen nstudies3=0
gen nstudies4=0
forvalues member=1(1)18 {
	replace nstudies1=nstudies1+(ESTUDRED`member'==1)
	replace nstudies2=nstudies2+(ESTUDRED`member'==2)
	replace nstudies3=nstudies3+(ESTUDRED`member'==3)
	replace nstudies4=nstudies4+(ESTUDRED`member'==4)
}
*
* Part/Full time employed household members
*
gen nptime=0 if ANOENC>2010
gen nftime=0 if ANOENC>2010
gen nptimefemale=0 if ANOENC>2010
gen nftimefemale=0 if ANOENC>2010
gen nptimembsp=0 if ANOENC>2010
gen nftimembsp=0 if ANOENC>2010
forvalues member=1(1)18 {
	replace nptime=nptime+(JORNADA`member'==2) if ANOENC>2010
	replace nftime=nftime+(JORNADA`member'==1) if ANOENC>2010
	replace nptimefemale=nptimefemale+(JORNADA`member'==2 & SEXO`member'==6) if ANOENC>2010
	replace nftimefemale=nftimefemale+(JORNADA`member'==1 & SEXO`member'==6) if ANOENC>2010
	replace nptimembsp=nptimembsp+(JORNADA`member'==2 & (RELASP`member'==1 | RELASP`member'==2)) if ANOENC>2010
	replace nftimembsp=nftimembsp+(JORNADA`member'==1 & (RELASP`member'==1 | RELASP`member'==2)) if ANOENC>2010
}
*
* Number of hh members receiving income from any source
*
gen nincome=0 
forvalues member=1(1)18 {
	replace nincome=nincome+(PERCEP`member'==1)
}
*
* Number of active, inactive, employed, and unemployed household members
* Number of retired household members (main breadwinner or spouse)
*
gen nunemployed=0
gen nemployed=0
gen nactive=0
gen ninactive=0
gen nretired=0
gen nretiredmen=0
gen nretiredwomen=0
gen nretiredstud1=0
gen nretiredstud2=0
gen nretiredstud3=0
gen nretiredstud4=0
gen retiredhh=0
gen retiredsp2=0
forvalues member=1(1)18 {
	replace nunemployed=nunemployed+(SITUACT`member'==3)
	replace nemployed=nemployed+(SITUACT`member'==1 | SITUACT`member'==2)
	replace nactive=nactive+(SITUACT`member'==1 | SITUACT`member'==2 | SITUACT`member'==3)
	replace ninactive=ninactive+(SITUACT`member'==4 | SITUACT`member'==5 | ///
	SITUACT`member'==6 |SITUACT`member'==7 | SITUACT`member'==8)
	replace nretired=nretired+(SITUACT`member'==4 & (RELASP`member'==1 | RELASP`member'==2))
	replace nretiredmen=nretiredmen+(SITUACT`member'==4 & SEXO`member'==1 & ///
	(RELASP`member'==1 | RELASP`member'==2))
	replace nretiredwomen=nretiredwomen+(SITUACT`member'==4 & SEXO`member'==6 & ///
	(RELASP`member'==1 | RELASP`member'==2))
	replace nretiredstud1=nretiredstud1+(SITUACT`member'==4 & ESTUDRED`member'==1)
	replace nretiredstud2=nretiredstud2+(SITUACT`member'==4 & ESTUDRED`member'==2)
	replace nretiredstud3=nretiredstud3+(SITUACT`member'==4 & ESTUDRED`member'==3)
	replace nretiredstud4=nretiredstud4+(SITUACT`member'==4 & ESTUDRED`member'==4)
	if RELASP`member'==1 {
		replace retiredhh=1 if SITUACT`member'==4
	}
	if RELASP`member'==2 {
		replace retiredsp2=1 if SITUACT`member'==4
	}
}
*
* Unemployed main breadwinner or spouse
*
gen umbsp=.
replace umbsp=(unemployedhousehead==1 | unemployedspouse==1)
*
* Number of unemployed household members by gender
*
gen nunemployedboys=0
gen nunemployedgirls=0
forvalues member=1(1)18 {
	replace nunemployedboys=nunemployedboys+(SITUACT`member'==3 & SEXO`member'==1)
	replace nunemployedgirls=nunemployedgirls+(SITUACT`member'==3 & SEXO`member'==6)
}
*
* Number of unemployed household members by education level 
*
gen nunemstud1=0
gen nunemstud2=0
gen nunemstud3=0
gen nunemstud4=0
forvalues member=1(1)18 {
	replace nunemstud1=nunemstud1+(SITUACT`member'==3 & ESTUDRED`member'==1)
	replace nunemstud2=nunemstud2+(SITUACT`member'==3 & ESTUDRED`member'==2)
	replace nunemstud3=nunemstud3+(SITUACT`member'==3 & ESTUDRED`member'==3)
	replace nunemstud4=nunemstud4+(SITUACT`member'==3 & ESTUDRED`member'==4)
}
*
* TREATMENTS
*
* Unemployment: T01 & T12
*
gen t01=.
replace t01=1 if nunemployed==1 & l.nunemployed==0 & l.nactive>0 & d.nactive==0
replace t01=0 if nunemployed==0 & l.nunemployed==0 & l.nactive>0 & d.nactive==0
tab t01
gen t12=.
replace t12=1 if nunemployed==2 & l.nunemployed==1 & l.nactive>1 & d.nactive==0
replace t12=0 if nunemployed==1 & l.nunemployed==1 & l.nactive>1 & d.nactive==0
tab t12
*
* Inverse treatments
*
gen t10=.
replace t10=1 if nunemployed==0 & l.nunemployed==1 & d.nactive==0
replace t10=0 if nunemployed==1 & l.nunemployed==1 & d.nactive==0
tab t10
gen t21=.
replace t21=1 if nunemployed==1 & l.nunemployed==2 & d.nactive==0
replace t21=0 if nunemployed==2 & l.nunemployed==2 & d.nactive==0
tab t21
*
* Unemployment by gender
*
gen t01boys=.
replace t01boys=(d.nunemployedboys==1 & t01==1) 
replace t01boys=. if t01==.
tab t01boys
gen t01girls=.
replace t01girls=(d.nunemployedgirls==1 & t01==1) 
replace t01girls=. if t01==.
tab t01girls
gen t12boys=.
replace t12boys=(d.nunemployedboys==1 & t12==1) 
replace t12boys=. if t12==.
tab t12boys
gen t12girls=.
replace t12girls=(d.nunemployedgirls==1 & t12==1) 
replace t12girls=. if t12==.
tab t12girls
gen t10boys=.
replace t10boys=(d.nunemployedboys==-1 & t10==1) 
replace t10boys=. if t10==.
tab t10boys
gen t10girls=.
replace t10girls=(d.nunemployedgirls==-1 & t10==1) 
replace t10girls=. if t10==.
tab t10girls
gen t21boys=.
replace t21boys=(d.nunemployedboys==-1 & t21==1) 
replace t21boys=. if t21==.
tab t21boys
gen t21girls=.
replace t21girls=(d.nunemployedgirls==-1 & t21==1) 
replace t21girls=. if t21==.
tab t21girls
*
* unemployment by studies
*
gen t01stud1=.
replace t01stud1=(d.nunemstud1==1 & t01==1) 
replace t01stud1=. if t01==.
tab t01stud1
gen t01stud2=.
replace t01stud2=(d.nunemstud2==1 & t01==1) 
replace t01stud2=. if t01==.
tab t01stud2
gen t01stud3=.
replace t01stud3=(d.nunemstud3==1 & t01==1) 
replace t01stud3=. if t01==.
tab t01stud3
gen t01stud4=.
replace t01stud4=(d.nunemstud4==1 & t01==1) 
replace t01stud4=. if t01==.
tab t01stud4

gen t12stud1=.
replace t12stud1=(d.nunemstud1==1 & t12==1) 
replace t12stud1=. if t12==.
tab t12stud1
gen t12stud2=.
replace t12stud2=(d.nunemstud2==1 & t12==1) 
replace t12stud2=. if t12==.
tab t12stud2
gen t12stud3=.
replace t12stud3=(d.nunemstud3==1 & t12==1) 
replace t12stud3=. if t12==.
tab t12stud3
gen t12stud4=.
replace t12stud4=(d.nunemstud4==1 & t12==1) 
replace t12stud4=. if t12==.
tab t12stud4

gen t10stud1=.
replace t10stud1=(d.nunemstud1==-1 & t10==1) 
replace t10stud1=. if t10==.
tab t10stud1
gen t10stud2=.
replace t10stud2=(d.nunemstud2==-1 & t10==1) 
replace t10stud2=. if t10==.
tab t10stud2
gen t10stud3=.
replace t10stud3=(d.nunemstud3==-1 & t10==1) 
replace t10stud3=. if t10==.
tab t10stud3
gen t10stud4=.
replace t10stud4=(d.nunemstud4==-1 & t10==1) 
replace t10stud4=. if t10==.
tab t10stud4

gen t21stud1=.
replace t21stud1=(d.nunemstud1==-1 & t21==1) 
replace t21stud1=. if t21==.
tab t21stud1
gen t21stud2=.
replace t21stud2=(d.nunemstud2==-1 & t21==1) 
replace t21stud2=. if t21==.
tab t21stud2
gen t21stud3=.
replace t21stud3=(d.nunemstud3==-1 & t21==1) 
replace t21stud3=. if t21==.
tab t21stud3
gen t21stud4=.
replace t21stud4=(d.nunemstud4==-1 & t21==1) 
replace t21stud4=. if t21==.
tab t21stud4
*
* Employed to retirement
*
gen tr=.
replace tr=1 if nretired==1 & l.nretired==0 & l.nemployed>0 & d.nemployed==-1 & d.nactive==-1 & l.n50ormore>0
replace tr=0 if nretired==0 & l.nretired==0 & l.nemployed>0 & d.nemployed==0 & d.nactive==0 & l.n50ormore>0
tab tr
*
* Employed to retirement by gender
*
gen trboys=tr*(nretiredmen==1)
tab trboys
*
gen trgirls=tr*(nretiredwomen==1)
tab trgirls
*
* Temporary to permanent contract (Main breadwinner only)
*
gen ttp=.
replace ttp=1 if perm==1 & l.temp==1 & d.nmembers==0 
replace ttp=0 if temp==1 & l.temp==1 & d.nmembers==0 
tab ttp
*
* Temporary to permanent by gender
*
gen ttpboys=ttp*(femalemb==0)
tab ttpboys
gen ttpgirls=ttp*(femalemb==1)
tab ttpgirls
*
* Part-time to Full-time employment
*
gen tpf=.
replace tpf=1 if d.nftime==1 & d.nptime==-1 & l.nptime>0 & d.nemployed==0 & d.nactive==0
replace tpf=0 if d.nftime==0 & d.nptime==0 & l.nptime>0 & d.nemployed==0 & d.nactive==0
tab tpf
*
* Full-time to Part-time employment
*
gen tfp=.
replace tfp=1 if d.nftime==-1 & d.nptime==1 & l.nftime>0 & d.nemployed==0 & d.nactive==0
replace tfp=0 if d.nftime==0 & d.nptime==0 & l.nftime>0 & d.nemployed==0 & d.nactive==0
tab tfp

*
* Part-time to Full-time employment mainbreadwinner or spouse
*
gen tpfmbsp=.
replace tpfmbsp=1 if nptimembsp==0 & l.nptimembsp==1 & d.nftimembsp==1 
replace tpfmbsp=0 if nptimembsp==1 & l.nptimembsp==1 & d.nftimembsp==0 
tab tpfmbsp

*
* When we estimate treatment effects, time series operators are not allowed, so we define 
* treatments with capital T the same treatment only forwarded 
* Doing so allows us to run all regressions using observations from just the first survey participation 
*
gen T01=f.t01
label variable T01 "Treatment T01"
gen T12=f.t12
label variable T12 "Treatment T12"
gen T10=f.t10
label variable T10 "Treatment T10"
gen T21=f.t21
label variable T21 "Treatment T21"
gen T01BOYS=f.t01boys
label variable T01BOYS "Treatment T01 Male"
gen T01GIRLS=f.t01girls
label variable T01GIRLS "Treatment T01 Female"
gen T12BOYS=f.t12boys
label variable T12BOYS "Treatment T12 Male"
gen T12GIRLS=f.t12girls
label variable T12GIRLS "Treatment T12 Female"
gen T10BOYS=f.t10boys
label variable T10BOYS "Treatment T10 Male"
gen T10GIRLS=f.t10girls
label variable T10GIRLS "Treatment T10 Female"
gen T21BOYS=f.t21boys
label variable T21BOYS "Treatment T21 Male"
gen T21GIRLS=f.t21girls
label variable T21GIRLS "Treatment T21 Female"
gen TR=f.tr
label variable TR "Treatment Retirement"
gen TRBOYS=f.trboys
label variable TRBOYS "Treatment Retirement Man"
gen TRGIRLS=f.trgirls
label variable TRGIRLS "Treatment Retirement Woman"
gen TTP=f.ttp
label variable TTP "Treatment Temporary to Permanent contract"
gen TTPBOYS=f.ttpboys
label variable TTPBOYS "Treatment Temporary to Permanent contract Male"
gen TTPGIRLS=f.ttpgirls
label variable TTPGIRLS "Treatment Temporary to Permanent contract Female"

gen T01STUD1=f.t01stud1
label variable T01STUD1 "Treatment T01 studies 1"
gen T01STUD2=f.t01stud2
label variable T01STUD2 "Treatment T01 studies 2"
gen T01STUD3=f.t01stud3
label variable T01STUD3 "Treatment T01 studies 3"
gen T01STUD4=f.t01stud4
label variable T01STUD4 "Treatment T01 studies 4"

gen T12STUD1=f.t12stud1
label variable T12STUD1 "Treatment T12 studies 1"
gen T12STUD2=f.t12stud2
label variable T12STUD2 "Treatment T12 studies 2"
gen T12STUD3=f.t12stud3
label variable T12STUD3 "Treatment T12 studies 3"
gen T12STUD4=f.t12stud4
label variable T12STUD4 "Treatment T12 studies 4"

gen T10STUD1=f.t10stud1
label variable T10STUD1 "Treatment T10 studies 1"
gen T10STUD2=f.t10stud2
label variable T10STUD2 "Treatment T10 studies 2"
gen T10STUD3=f.t10stud3
label variable T10STUD3 "Treatment T10 studies 3"
gen T10STUD4=f.t10stud4
label variable T10STUD4 "Treatment T10 studies 4"

gen T21STUD1=f.t21stud1
label variable T21STUD1 "Treatment T21 studies 1"
gen T21STUD2=f.t21stud2
label variable T21STUD2 "Treatment T21 studies 2"
gen T21STUD3=f.t21stud3
label variable T21STUD3 "Treatment T21 studies 3"
gen T21STUD4=f.t21stud4
label variable T21STUD4 "Treatment T21 studies 4"



gen TPF=f.tpf
label variable TPF "Treatment Part-time to Full-time"
gen TFP=f.tfp
label variable TFP "Treatment Full-time to Part-time"
gen TPFMBSP=f.tpfmbsp
label variable TPF "Treatment Part-time to Full-time (Main breadwinner or spouse)"
*
* Same for survey weights
*
gen factor=f.FACTOR
*
* Deflating/inflating expenditure data and computing budget shares
*
replace ipc=ipc/100
gen gastot=0
forvalues group=1/12 {
	replace ipc`group'=ipc`group'/100
	replace gasto`group'=gasto`group'/ipc`group'
	gen gastoaeq`group'=gasto`group'/nmembaeq
	replace gastot=gastot+gasto`group'
}
*first differencing outcomes
* total expenditure
gen dgastot=f.d.gastot
* total adult equivalent expenditure
gen gastotaeq=gastot/nmembaeq
gen dgastotaeq=f.d.gastotaeq
* real yearly income 
replace income=12*income/ipc
gen dincome=f.d.income 
* adult equivalent income
gen incomeaeq=income/nmembaeq
gen dincomeaeq=f.d.incomeaeq

sort year id_hh
by year: egen sumFACTOR=sum(FACTOR)
gen newfactor=FACTOR/sumFACTOR
forvalues group=1/12 {
	* expenditure share should not be indexed to inflation
	* gen share`group'=gasto`group'/GASTOT
	gen share`group'=gasto`group'/gastot
	gen auxshare`group'=share`group'*newfactor
	by year: egen sharemean`group'=sum(auxshare`group')
}
*
* First differences of outcomes 
*
* order variables
order gasto* share*, sequential
*
sort id_hh year
forvalues cat=1/12 {
	gen dgasto`cat'=f.d.gasto`cat'
	gen dgastoaeq`cat'=f.d.gastoaeq`cat'
	gen dshare`cat'=f.d.share`cat'
}
*
* Label first difference expenditure data
*
label variable dgasto1 "01 Alimentos y bebidas no alcohólicas"
label variable dgasto2 "02 Bebidas alcohólicas y tabaco"
label variable dgasto3 "03 Vestido y calzado"
label variable dgasto4 "04 Vivienda, agua, electricidad, gas y otros combustibles"
label variable dgasto5 "05 Muebles, artículos del hogar y artículos para el mantenimiento corriente del hogar"
label variable dgasto6 "06 Sanidad"
label variable dgasto7 "07 Transporte"
label variable dgasto8 "08 Comunicaciones"
label variable dgasto9 "09 Ocio y cultura"
label variable dgasto10 "10 Enseñanza"
label variable dgasto11 "11 Restaurantes y hoteles"
label variable dgasto12 "12 Otros bienes y servicios"
label variable dgastot "Gasto Total"
label variable dincome "Ingreso"
*
drop A* C* D* E* F* G* H* I* J* M* N* O* P* R* S* U* Z*
drop TAMAMU TAMANO TIPHOGAR_* TIPOCASA TIPOCONT TIPOEDIF TRABAJO
save epfipc2006-2019plus.dta, replace
*
* The End
*
